#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_FILENUKE_REGEXP_URL="http://\(www\.\)\?filenuke\.com/*/"

MODULE_FILENUKE_DOWNLOAD_OPTIONS=""
MODULE_FILENUKE_DOWNLOAD_RESUME=yes
MODULE_FILENUKE_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_FILENUKE_LIST_OPTIONS=""

filenuke_download() {

local COOKIEFILE=$1
local URL=$2

	Pageinit=$(curl "$URL") || return
  values=$(echo "$Pageinit" | grep 'value="' | head -n -3)
  op=$(echo "$values" | parse_form_input_by_name 'op')
  id=$(echo "$values" | parse_form_input_by_name 'id')
  fname=$(echo "$values" | parse_form_input_by_name 'fname')

	downloadbtnpage=$(curl -c $COOKIEFILE -e $URL -d "op=$op&usr_login=&id=$id&fname=$fname&referer=&method_free=Free" $URL) || return
	
	echo $downloadbtnpage > downloadbtnpage
	
	values=$(echo "$downloadbtnpage" | grep 'value="')
	op=$(echo "$values" | parse_form_input_by_name 'op')
	rand=$(echo "$values" | parse_form_input_by_name 'rand')
	
	LINK_HEADER=$(curl -i -b $COOKIEFILE -e $URL -d "op=$op&id=$id&rand=$rand&referer=$URL&method_free=Free&method_premium=&down_script=1" $URL)
	
  FILE_URL=$(echo "$LINK_HEADER" | grep 'Location')
  

  FILE_URL=${FILE_URL:10}
	FILENAME="$fname"
	
	echo "$FILE_URL"
	echo "$FILENAME"
}